package kr.pe.osdev.dao.jdbc.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import kr.pe.osdev.dao.IBaseDao;
import kr.pe.osdev.dao.jdbc.BaseJdbcDao;
import kr.pe.osdev.domain.User;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;

@SuppressWarnings("deprecation")
//@Component("userDaoImpl")
@Deprecated
public class UserDaoComImpl extends BaseJdbcDao implements IBaseDao {
	protected final Log logger = LogFactory.getLog(getClass());
	
	@Override
	public User add(Object o) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("user.addUser");
//        user.setCreatDate(String.valueOf(System.currentTimeMillis()));
//        user.setLastLogin(user.getCreatDate());
        User user = (User) o;
        
        Object[] obj = new Object[] { user.getSid(),
                user.getPid(),
                user.getUserId(),
                user.getPasswd(),
                user.getEmail(),
                user.getUserName(),
                user.getNickName(),
                user.getImgFile(),
                user.getIconFile()};
        
        getJdbcTemplate().update(sql, obj);
        
        return user;
	}

	@Override
	public User find(Object o) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("user.findUser");
        User user = (User) o;
        return (User) getJdbcTemplate().queryForObject(sql, new Object[] { user.getPid(), user.getUserId() }, getRowMapper());
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Object> findList() throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("user.findUserList");

        return (List<Object>) getJdbcTemplate().query(sql, getRowMapper());
	}

	@Override
	public int count() throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("user.count");
        
		return getJdbcTemplate().queryForInt(sql);
	}

	@Override
	public User modify(Object o) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("user.modifyUser");
        
        User user = (User) o;
        
        Object[] obj = new Object[] { user.getPasswd(),
    								user.getEmail(),
    								user.getUserName(),
    								user.getNickName(),
    								user.getImgFile(),
    								user.getIconFile(),
    								user.getSid()};
  
        getJdbcTemplate().update(sql, obj);
      
		return user;
	}

	public User remove(User user) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("user.removeUser");
        
        getJdbcTemplate().update(sql, new Object[] { user.getSid() });
        
        return user;
	}

    private RowMapper getRowMapper() {
        RowMapper rowMapper = new RowMapper() {
            public Object mapRow(ResultSet rs, int rownum) throws SQLException {
                User user = new User();
                user.setSid(rs.getString("SID"));
                user.setPid(rs.getString("PID"));
                user.setPasswd(rs.getString("PASSWD"));
                user.setUserId(rs.getString("USER_ID"));
                user.setPasswd(rs.getString("PASSWD"));
                user.setEmail(rs.getString("EMAIL"));
                user.setUserName(rs.getString("USER_NAME"));
                user.setNickName(rs.getString("NICK_NAME"));
                user.setImgFile(rs.getString("IMG_FILE"));
                user.setIconFile(rs.getString("ICON_FILE"));

                return user;
            }
        };
        
        return rowMapper;
    }
}
